package com.ydd.controller.user;

import com.baomidou.mybatisplus.extension.api.R;
import com.ydd.constant.JwtClaimsConstant;
import com.ydd.context.BaseContext;
import com.ydd.dto.ArticlePageDTO;
import com.ydd.dto.SysUserLoginDTO;
import com.ydd.dto.UserRegDTO;
import com.ydd.dto.YddUserLoginDTO;
import com.ydd.entity.YddUser;
import com.ydd.properties.JwtProperties;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.IArticleService;
import com.ydd.service.IConfigService;
import com.ydd.service.IYddUserService;
import com.ydd.utils.JwtUtil;
import com.ydd.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author itbobo
 * @Date 2023-05-31 18:12
 */
@Api(tags = "前台接口基础数据相关")
@Slf4j
@RestController("apiIndexController")
@RequestMapping("/api")
public class IndexController {

    @Autowired
    private IArticleService articleService;

    /**
     * 热门置顶文章数据(首页)接口
     * @return
     */
    @ApiOperation("热门置顶文章数据(首页)接口")
    @RequestMapping("/index/new")
    public Result<NewInfoVO> getNew(Integer page, Integer limit){
        //打印日志信息
        log.info("热门顶置文章数据...");
        NewInfoVO newInfoVO = articleService.getNewInfo(page, limit);
        //返回数据
        return Result.success(newInfoVO);
    }

    /**
     * 文章列表数据接口
     * @param articlePageDTO
     * @return
     */
    @ApiOperation("文章列表数据接口")
    @GetMapping("/index/list")
    public Result<PageResult> list(ArticlePageDTO articlePageDTO){
        //打印日志信息
        log.info("文章列表数据...");
        PageResult pageResult = articleService.getArticleList(articlePageDTO);
        //返回信息
        return Result.success(pageResult);
    }

    /**
     * 文章详情数据接口
     * @param id
     * @return
     */
    @ApiOperation("文章详情数据接口")
    @GetMapping("/index/show")
    public Result<ArticleShowVO> show(Integer id){
        //打印日志信息
        log.info("文章详情数据:{}", id);
        //调用业务层方法
        ArticleShowVO articleShowVO = articleService.show(id);
        //返回数据
        return Result.success(articleShowVO);
    }

    /**
     * 最新文章
     * @return
     */
    @ApiOperation("最新文章")
    @GetMapping("/index/hot")
    public Result<ArticleResult> hot(){
        //打印日志信息
        log.info("最新文章...");
        //调用业务层方法
        ArticleResult hotArticleResult = articleService.hot();
        //返回信息
        return Result.success(hotArticleResult);
    }

    /**
     * 推荐文章
     * @return
     */
    @ApiOperation("推荐文章接口")
    @GetMapping("/index/recommend")
    public Result<ArticleResult> recommend(){
        //打印日志信息
        log.info("推荐文章...");
        //调用业务层方法
        ArticleResult articleResult = articleService.recommend();
        //返回信息
        return Result.success(articleResult);
    }

    @Autowired
    private IYddUserService yddUserService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户注册
     * @param userRegDTO
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/index/reg")
    public Result reg(@RequestBody UserRegDTO userRegDTO){
        //打印日志信息
        log.info("用户注册:{}", userRegDTO);
        //调用业务层方法
        Integer id = yddUserService.reg(userRegDTO);

        //注册成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, id);
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        //返回token
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        //返回信息
        return Result.success(map, "注册成功");
    }

    /**
     * 获取登录用户资料
     * @return
     */
    @ApiOperation("获取登录用户资料")
    @GetMapping("/user/getUserInfo")
    public Result getUserInfo(){
        //打印日志信息
        log.info("获取登录用户资料...");
        String userId = BaseContext.getCurrentId();
        Integer id = Integer.valueOf(userId);
        //调用业务层方法
        UserInfoVO userInfoVO = yddUserService.getUserInfo(id);
        //返回数据
        return Result.success(userInfoVO);
    }

    @Autowired
    private IConfigService configService;

    /**
     * 首页和全局数据
     * @return
     */
    @ApiOperation("首页和全局数据")
    @GetMapping("/index/index")
    public Result<IndexResult> index(){
        //打印日志信息
        log.info("首页和全局数据...");
        //调用业务层方法
        IndexResult indexResult = configService.index();
        //返回数据
        return Result.success(indexResult);
    }
}
